const express = require("express");
const app = express.Router();

function getRandomId() {
  return Math.floor(Math.random() * 1000000);
}

function dataChange(pageNumber = 1, pageSize = 10, data) {
  const startIndex = (Number(pageNumber) - 1) * Number(pageSize);
  const endIndex = startIndex + Number(pageSize);
  // 获取当前页的数据
  const currentData = data.slice(startIndex, endIndex);
  // 构建分页结果
  const result = {
    code: 0,
    data: {
      record: currentData,
      pageNumber: +pageNumber,
      pageSize: +pageSize,
      totalItems: data.length,
      totalPages: Math.ceil(data.length / pageSize),
    },
  };
  return result;
}
const menuData = [
  {
    name: "宫保鸡丁",
    price: 100,
    id: 9057633,
  },
  {
    name: "麻辣香锅",
    price: 120,
    id: 644825,
  },
  {
    name: "红烧肉",
    price: 80,
    id: 8826524,
  },
  {
    name: "水煮鱼",
    price: 150,
    id: 3066228,
  },
  {
    name: "蒜蓉生蚝",
    price: 180,
    id: 1710463,
  },
  {
    name: "北京炸酱面",
    price: 60,
    id: 8769298,
  },
  {
    name: "黄焖鸡米饭",
    price: 90,
    id: 6263049,
  },
  {
    name: "辣子鸡",
    price: 110,
    id: 5217601,
  },
  {
    name: "清蒸鲈鱼",
    price: 130,
    id: 7447933,
  },
  {
    name: "番茄炒蛋",
    price: 50,
    id: 3849124,
  },
  {
    name: "酸辣汤",
    price: 40,
    id: 9199821,
  },
  {
    name: "糖醋排骨",
    price: 120,
    id: 7950761,
  },
  {
    name: "香菇青菜炒鸡肉",
    price: 70,
    id: 5605084,
  },
  {
    name: "梅菜扣肉",
    price: 95,
    id: 9394147,
  },
  {
    name: "红烧茄子",
    price: 65,
    id: 7804075,
  },
  {
    name: "葱姜炒蟹",
    price: 200,
    id: 1459963,
  },
  {
    name: "冰糖炖梨",
    price: 30,
    id: 850852,
  },
  {
    name: "蚝油生菜",
    price: 45,
    id: 9810971,
  },
  {
    name: "牛肉拉面",
    price: 85,
    id: 5617500,
  },
  {
    name: "炸鸡翅",
    price: 75,
    id: 595000,
  },
];

app.get("/menuListByPage", (req, res) => {
  // 模拟JSON数据
  let resultData = menuData;
  const filters = req.query;
  if (Object.keys(filters).length > 0) {
    resultData = menuData.filter((product) =>
      Object.keys(filters).every((key) => {
        // 没有直接返回true
        if (!product[key]) {
          return true;
        }
        return String(product[key])
          .toLowerCase()
          .includes(String(filters[key]).toLowerCase());
      })
    );
  }

  const data = dataChange(req.query.pageNumber, req.query.pageSize, resultData);
  // 设置响应头
  res.setHeader("Content-Type", "application/json");

  // 发送JSON数据
  res.send(JSON.stringify(data));
});

app.get("/menuList", (req, res) => {
  // 模拟JSON数据
  const resultData = {
    code: 0,
    data: menuData,
  };
  // 设置响应头
  res.setHeader("Content-Type", "application/json");

  // 发送JSON数据
  res.send(JSON.stringify(resultData));
});

app.post("/createMenu", (req, res) => {
  // 模拟JSON数据
  const jsonData = { code: 0, data: null, msg: "新增成功" };
  const newMenuData = req.body;
  if (!newMenuData || !newMenuData.name || !newMenuData.price) {
    throw "数据不正确";
  }
  menuData.unshift({ id: getRandomId(), ...newMenuData });
  // 设置响应头
  res.setHeader("Content-Type", "application/json");

  // 发送JSON数据
  res.send(JSON.stringify(jsonData));
});

app.put("/editMenu", (req, res) => {
  // 模拟JSON数据
  const jsonData = { code: 0, data: null, msg: "修改成功" };
  const newMenuData = req.body;
  if (
    !newMenuData ||
    !newMenuData.name ||
    !newMenuData.price ||
    !newMenuData.id
  ) {
    throw "数据不正确";
  }
  const index = menuData.findIndex((item) => item.id == newMenuData.id);
  if (index === -1) {
    throw "id 不正确";
  }
  console.log(newMenuData);
  menuData[index] = newMenuData;
  // 设置响应头
  res.setHeader("Content-Type", "application/json");

  // 发送JSON数据
  res.send(JSON.stringify(jsonData));
});

app.delete("/deleteMenu/:id", (req, res) => {
  // 模拟JSON数据
  const jsonData = { code: 0, data: null, msg: "删除成功" };
  const menuId = req.params.id;
  if (!menuId) {
    throw "数据不正确";
  }
  const index = menuData.findIndex((item) => item.id == menuId);
  if (index === -1) {
    throw "未找到ID";
  }
  menuData.splice(index, 1);
  // 设置响应头
  res.setHeader("Content-Type", "application/json");

  // 发送JSON数据
  res.send(JSON.stringify(jsonData));
});

module.exports = app;
